package com.jhws.sys.appUser.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jhws.sys.appUser.bo.TblSipAcountDimBo;
import com.jhws.util.GetId;
import com.jhws.core.db.BaseDao;
import com.jhws.core.db.BeanResultSetExtrator;
import com.jhws.core.exception.BizException;


/**
 * 小区相关服务
 *
 * @author xiaokek
 * @since 2015年11月29日 上午10:31:43
 */
@Service
public class AppUserService {
	private BaseDao dao = new BaseDao();
	public void updatePwd(String findPwd, String username, String oldPassword, String newPassword) {
		if(StringUtils.isEmpty(oldPassword)){
			oldPassword = "";
		}
		TblSipAcountDimBo sip = dao.j().query("select * from tbl_sipacount_dim where username = ?", new Object[]{username}, new BeanResultSetExtrator<TblSipAcountDimBo>(TblSipAcountDimBo.class));

		if(sip == null)
			throw new BizException("-1","modifyPassword", "用户不存在");
		
		if(!findPwd.equals("true") && !oldPassword.equals(sip.getPassword())){
			throw new BizException("-1","modifyPassword", "旧密码不正确");
		}
		
		dao.j().update("update tbl_sipacount_dim set password = ? where username = ?", newPassword,username);
	}
	
	public List<String> queryMobile(String roomInfo){
		return dao.j().queryForList("select owner_mob from tbl_owner_dim where room_info_num = ?", new Object[]{roomInfo}, String.class);
	}
	
	public void addAccount(String username, String password){
		System.out.println("username: "+username+"  password: "+password);
		TblSipAcountDimBo sip = dao.j().query("select * from tbl_sipacount_dim where username = ?", new Object[]{username}, new BeanResultSetExtrator<TblSipAcountDimBo>(TblSipAcountDimBo.class));
		if(sip != null)
			throw new BizException("-1","registeredAccount", "用户已经存在！");
		dao.j().execute("insert into tbl_sipacount_dim (acount_id,username,password,type)values('"+GetId.randomID()+"','"+username+"','"+password+"','mobile')");
	}
}
